package com.prolificinteractive.materialcalendarview.sample.decorators;

import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.ResourceTable;

import java.io.IOException;
import java.util.HashSet;

/**
 * Use a custom selector
 */
public class MySelectorDecorator implements DayViewDecorator {
    private Element drawable;

    public MySelectorDecorator(Context context) {
        try {
            Resource bgResource = context.getResourceManager().getResource(ResourceTable.Media_ic_my_selector);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(bgResource, srcOpts);
            PixelMap pixelMap = imageSource.createPixelmap(null);
            drawable = new PixelMapElement(pixelMap);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return true;
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.setSelectionDrawable(drawable);
        view.setBig(true);
    }
}
